<?php
	session_start();
	require('../core/config.php');
	
	if(isset($_SESSION['islogin'])){
		if($_GET['action']=='subarticle'){
			$titleimg = isset($_POST['titleimg'])?$_POST['titleimg']:NULL;
			if(isset($_POST['time'])){
				$time = $_POST['time'];
			}else{
				$time = time();
			}
			if(subArticle($_POST['title'],$_POST['content'],$_POST['artkind'],$_POST['state'],$_POST['author'],$time,$_POST['istop'],$_POST['isbanr'],$titleimg,$_POST['isshow'],$_POST['ispage'])==true){
				echo 1;
			}else{
				echo 0;
			}
		}elseif($_GET['action']=='uparticle'){
			$titleimg = isset($_POST['titleimg'])?$_POST['titleimg']:NULL;
			//更新时不再设置时间
			//if(isset($_POST['time'])){
			//	$time = $_POST['time'];
			//}else{
			//	$time = time();
			//}
			$time = "呵呵";
			if(subArticle($_POST['title'],$_POST['content'],$_POST['artkind'],$_POST['state'],$_POST['author'],$time,$_POST['istop'],$_POST['isbanr'],$titleimg,$_POST['isshow'],$_POST['ispage'],1,$_POST['id'])==true){
				echo 1;
			}else{
				echo 0;
			}			
		}elseif($_GET['action']=='unsetarticle'){
			if(unsetArticle($_GET['id'])==true){
				echo "<script>location.replace('article.php?')</script>";
			}
		}
		elseif($_GET['action']=='settings'){
			foreach($_POST as $key=>$value){
				updateSettings($key,$value);
			}
			echo "<script>location.replace('settings.php?')</script>";
		}elseif($_GET['action']=='subarticleimg'){
			$pass = checkFile($_FILES);
			if($pass==0){
				$base = getImgNumber();
				$destination = "../source/picture/";
				foreach($_FILES as $key=>$value){
					$base++;
					$name = explode('.',$value['name']);
					$name = $base.'.'.$name[count($name)-1];
					$des = $destination.$name;
					move_uploaded_file($value['tmp_name'],$des);
				}
				updateImgNumber(count($_FILES));			
			}
			echo $name;
		}elseif($_GET['action']=='getarticletext'){
			$article = getArticle($_POST['id']);
			echo stripslashes($article['content']);
		}elseif($_GET['action']=='settopmenu'){
			if(addOneMenu($_POST)==true){
				echo 1;
			}
		}elseif($_GET['action']=='delmenu'){
			if(deleteTopMenu((int)$_GET['id'])==true){
				echo "<script>location.replace('settings.php?')</script>";
			}
		}elseif($_GET['action']=='deleteremark'){
			if(deleteRemark($_POST['id'],$_POST['artid'])==true){
				echo 1;
			}
		}elseif($_GET['action']=='addartkind'){
			if(addArtKind($_POST['artkindname'])==true){
				echo "<script>location.replace('artkind.php?')</script>";
			}			
		}elseif($_GET['action']=='updateartkind'){
			foreach($_POST as $key=>$value){
				updateArtKind($key,$value);
			}
			echo "<script>location.replace('artkind.php?')</script>";
		}elseif($_GET['action']=='deleteartkind'){
			if(deleteArtKind($_GET['id'])==true){
				echo "<script>location.replace('artkind.php?')</script>";
			}		
		}elseif($_GET['action']=='addlink'){
			//处理头像上传
			$pass = checkFile($_FILES);
			if($pass==0){
				$base = getImgNumber();
				$destination = "../source/picture/";
				foreach($_FILES as $key=>$value){
					$base++;
					$name = explode('.',$value['name']);
					$name = $base.'.'.$name[count($name)-1];
					$des = $destination.$name;
					move_uploaded_file($value['tmp_name'],$des);
				}
				updateImgNumber(count($_FILES));			
			}
			//提交内容到数据库
			if($_POST['color']=='蓝色'){
				$_POST['color'] = '#75baff';
			}elseif($_POST['color']=='粉色'){
				$_POST['color'] = '#F08080';
			}
			subLink($_POST['index'],$_POST['name'],$_POST['url'],$name,$_POST['introduce'],$_POST['color']);
			echo "<script>location.replace('link.php?')</script>";
		}
		elseif($_GET['action']=='deletelink'){
			deleteLink($_GET['id']);
			echo "<script>location.replace('link.php?')</script>";
		}
		elseif($_GET['action']=='updatelink'){
			$num = count($_POST['id']);
			for($i=0;$i<$num;$i++){
				$data = array();
				foreach($_POST as $key=>$value){
					if($key=='id'){
						$id = $_POST[$key][$i];
					}else{
						array_push($data,$_POST[$key][$i]);
					}
					
				}
				updateLink($id,$data);
			}
			echo "<script>location.replace('link.php?')</script>";
		}
		else{
			print_r($_POST);
		}

	}
?>